package com.cloud.basic.TcpAndUdp;


import java.io.*;
import java.net.SocketAddress;
import java.util.Arrays;

/**
 * @program: basic
 * @description: 请求发送消息实体的结构定义
 * @author: Cloud
 * @create: 2021/10/11 15:48:49
 */
public class RequestMessage {

    // 定义数据的长度
    private int totalLen;

    // 生产唯一id
    private int id;

    // 数据
    private byte[] data;

    // 发送次数
    private int sendCount = 0;

    // 最后一次发送时间
    private Long lastSendTime = System.currentTimeMillis();

    // 发送者接受应答的地址
    private SocketAddress recvRespAddr;

    // 接收者的地址
    private SocketAddress remoteAddr;

    public RequestMessage(int id, byte[] data){
        this.id = id;
        this.data = data;
        // 4+4 是因为每一个int类型的长度为4个字节
        this.totalLen = 4+4+data.length;
    }

    /**
     * 功能描述: 构造器接收到udp数据封装为一个tcp的RequestMessage对象
     *
     * @param udpData
     * @return null
     * @author cloud
     * @date  2021/10/11 16:04
     */
    public RequestMessage(byte[] udpData) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(udpData);
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        try {
            int readInt = dataInputStream.readInt();
//            this.id = dataInputStream.readInt();
            this.id = readInt;
//            this.totalLen = dataInputStream.readInt();
            this.totalLen = readInt;
            this.data = new byte[totalLen-4-4];
            dataInputStream.readFully(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public byte[] toByte(){
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(arrayOutputStream);
        try {
            // 读和写要一一对应
            dataOutputStream.writeInt(totalLen);
            dataOutputStream.writeInt(id);
            dataOutputStream.write(data);
            dataOutputStream.flush();
            return arrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getTotalLen() {
        return totalLen;
    }

    public void setTotalLen(int totalLen) {
        this.totalLen = totalLen;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getSendCount() {
        return sendCount;
    }

    public void setSendCount(int sendCount) {
        this.sendCount = sendCount;
    }

    public Long getLastSendTime() {
        return lastSendTime;
    }

    public void setLastSendTime(Long lastSendTime) {
        this.lastSendTime = lastSendTime;
    }

    public SocketAddress getRecvRespAddr() {
        return recvRespAddr;
    }

    public void setRecvRespAddr(SocketAddress recvRespAddr) {
        this.recvRespAddr = recvRespAddr;
    }

    public SocketAddress getRemoteAddr() {
        return remoteAddr;
    }

    public void setRemoteAddr(SocketAddress remoteAddr) {
        this.remoteAddr = remoteAddr;
    }


    @Override
    public String toString() {
        return "RequestMessage{" +
                "totalLen=" + totalLen +
                ", id=" + id +
                ", data=" + Arrays.toString(data) +
                ", sendCount=" + sendCount +
                ", lastSendTime=" + lastSendTime +
                ", recvRespAddr=" + recvRespAddr +
                ", remoteAddr=" + remoteAddr +
                '}';
    }
}
